﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using AutoMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using PeiYue.Api.Controllers.BASE;
using PeiYue.Common;
using PeiYue.Common.Helper;
using PeiYue.Common.HttpContextUser;
using PeiYue.IServices.Common;
using PeiYue.IServices.Tms;
using PeiYue.Model;
using PeiYue.Model.Dto.Tms;
using PeiYue.Model.Models.Common;
using PeiYue.Model.Models.Tms;

namespace PeiYue.Api.Controllers.Tms
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize(Permissions.Name)]
    public class RouterFeeController : BaseController<RouterFeeDao>
    {
        readonly IMapper _mapper;
        readonly IRouterFeeServices _routerFeeServices;
        IUser _user;
        readonly IRouterServices _routerServices;
        readonly IImgResDaoServices _imgResServices;
        readonly IRouterShopLogServices _routerShopLogServices;
        public RouterFeeController(IRouterFeeServices routerFeeServices, IUser user, IMapper mapper, IRouterServices routerServices, IImgResDaoServices imgResServices, IRouterShopLogServices routerShopLogServices) : base(routerFeeServices, user)
        {
            _routerFeeServices = routerFeeServices;
            _user = user;
            this._mapper = mapper;
            _routerServices = routerServices;
            _imgResServices = imgResServices;
            _routerShopLogServices = routerShopLogServices;
        }
        /// <summary>
        /// 查询分页列表
        /// </summary>
        /// <param name="page"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<MessageModel<PageModel<RouterFeeDto>>> GetPage([FromQuery] SearchRouterFeeDto querymodel)
        {
            Expression<Func<RouterFeeDao, bool>> ex = t => 1 == 1;

            if (querymodel.router_id > 0)
                ex = ex.And(t => t.router_id == querymodel.router_id);
            if (querymodel.status == null || querymodel.status == 0)
                ex = ex.And(t => t.status != 3);
            else
                ex = ex.And(t => t.status == querymodel.status);

            PageModel<RouterFeeDto> data = new PageModel<RouterFeeDto>() { };

            var list = await _routerFeeServices.QueryPage(ex, querymodel.pageIndex, querymodel.pageSize, " Id desc ");
            if (list.dataCount > 0)
            {
                long[] arr = list.data.Select(o => o.router_id).ToArray();
                var routers = await _routerServices.QueryByIDs(arr);

                List<ImgResDao> imgs = new List<ImgResDao>();
                if (list.data.Select(o => o.img_res_id).Any())
                    imgs = await _imgResServices.QueryByIDs(list.data.Select(o => o.img_res_id).ToArray());

                data = new PageModel<RouterFeeDto>() { dataCount = list.dataCount, page = list.page, PageSize = list.PageSize, data = _mapper.Map<List<RouterFeeDto>>(list.data) };

                data.data.ForEach(i => { i.router_name = routers.Where(r => r.id == i.router_id).FirstOrDefault()?.names; i.img_path = imgs.Where(m => m.id == i.img_res_id).FirstOrDefault()?.ImgPath; if (!string.IsNullOrEmpty(i.img_path)) i.img_path = Appsettings.app(new string[] { "FileUrl" }) + i.img_path; });
            }
            return new MessageModel<PageModel<RouterFeeDto>>()
            { msg = "获取成功", success = true, response = data };
        }

        /// <summary>
        /// 保存费用
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<MessageModel<string>> Save([FromBody] RouterFeeDto model)
        {
            var dao = _mapper.Map<RouterFeeDao>(model);
            dao.update_user = _user.ID;
            dao.create_user = _user.ID;
            dao.update_time = DateTime.Now;
            var data = new MessageModel<string>();
            if (model.id > 0)
            {

                data.success = await _routerFeeServices.Update(dao, null, new List<string> { "create_time", "create_user" });
            }
            else
            {
                dao.create_user = _user.ID;
                dao.create_time = DateTime.Now;
                dao.id = await _routerFeeServices.Add(dao);
                data.success = dao.id > 0;
            }
            if (data.success)
            {
                await _imgResServices.Updates(new { resid = dao.id, id = dao.img_res_id });
                data.msg = "保存成功";
                data.response = dao?.id.ObjToString();
                await _routerShopLogServices.UpdateRouterShopLog(model.router_id, model.fee_date,_user.ID);
            }
            return data;
        }
        ///// <summary>
        ///// 更新费用
        ///// </summary>
        ///// <param name="model"></param>
        ///// <returns></returns>
        //[HttpPost]
        //public async Task<MessageModel<string>> Update([FromBody] RouterFeeDto model)
        //{
        //    var data = new MessageModel<string>();
        //    var dao = _mapper.Map<RouterFeeDao>(model);
        //    if (dao != null && dao.id > 0)
        //    {
        //        dao.update_user = _user.ID;
        //        data.success = await _routerFeeServices.Update(dao, null, new List<string> { "status", "create_time", "create_user" });
        //        if (data.success)
        //        {
        //            data.msg = "更新成功";
        //            data.response = dao?.id.ObjToString();
        //        }
        //    }
        //    return data;
        //}

    }
}
